package com.weixin.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weixin.service.IKitingService;
import com.weixin.service.impl.KitingServiceImpl;
import com.weixin.util.PageUtil;
import com.weixin.vo.Kiting;
import com.weixin.vo.ZhuanZhang;

@WebServlet("/KitingServlet/*")
public class KitingServlet extends HttpServlet{
	IKitingService kitingService = new KitingServiceImpl();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pathInfo = request.getPathInfo().substring(1);
		if("findAll".equals(pathInfo)) {
			findAll(request,response);
		}else if("findBySplit".equals(pathInfo)) {
			findBySplit(request,response);
		}else if("removeById".equals(pathInfo)) {
			removeById(request,response);
		}else if("removeSelect".equals(pathInfo)) {
			removeSelect(request,response);
		}else if("selectByName".equals(pathInfo)) {
			selectByName(request,response);
		}
	}
	
	/**
	 * 通过姓名查找信息并且分页
	 * @param request
	 * @param response
	 */
	private void selectByName(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String user_name = request.getParameter("user_name");
			int pageNum = 1;
			if(request.getParameter("pageNum")!=null) {
				pageNum = Integer.valueOf(request.getParameter("pageNum"));
			}
			List<Kiting> kitingList= kitingService.selectByName(user_name, pageNum, PageUtil.PAGEUTIL);
			
			long total = kitingService.selectTotal(user_name);
	         //计算总页数
	        int pageCount = (int)Math.ceil(total*1.0/PageUtil.PAGEUTIL);
	        
	        // request对象绑定属性 ,表单回填
	        request.setAttribute("user_name", user_name);
	        request.setAttribute("pageCount",pageCount);
	        request.setAttribute("pageNum",pageNum);
	        request.setAttribute("kitingList",kitingList);
	        //转发到jsp处理页面
	        request.getRequestDispatcher("/pages/bank/withdrawal_details.jsp").forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除选中的所有的
	 * @param request
	 * @param response
	 */
	private void removeSelect(HttpServletRequest request, HttpServletResponse response) {
		String[] trade_ids = request.getParameterValues("trade_id");
        try {
            if(kitingService.removeSelect(trade_ids)){
                response.getWriter().println(1);
            }else{
                response.getWriter().println(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	/**
	 * 根据单号删除数据
	 * @param request
	 * @param response
	 */
	private void removeById(HttpServletRequest request, HttpServletResponse response) {
		int trade_id = Integer.valueOf( request.getParameter("trade_id"));
		try {
			if(kitingService.removeById(trade_id)) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 分页
	 * @param request
	 * @param response
	 */
	private void findBySplit(HttpServletRequest request, HttpServletResponse response) {
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		try {
			List<Kiting> kitingList = kitingService.findBySplit(pageNum, PageUtil.PAGEUTIL);
			 //计算总条数;
            int total = kitingService.findTotal();
            //计算总页数
            int pageCount = (int)Math.ceil(total*1.0/PageUtil.PAGEUTIL);
            // request对象绑定属性
            request.setAttribute("pageCount",pageCount);
            request.setAttribute("pageNum",pageNum);
            request.setAttribute("kitingList",kitingList);
            //转发到jsp处理页面
            request.getRequestDispatcher("/pages/bank/withdrawal_details.jsp").forward(request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 遍历所有的数据
	 * @param request
	 * @param response
	 */
	private void findAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Kiting> kitingList = kitingService.findALL();
			request.setAttribute("kitingList",kitingList);
			request.getRequestDispatcher("/pages/bank/withdrawal_details.jsp").forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
